package com.farm.business.exception;

import com.farm.business.utils.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.annotation.AccessType;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * 全局异常处理
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler  {

    /**
     * 拦截业务异常
     */
    @ExceptionHandler(BusinessException.class)
    public AjaxResult handleRuntimeException(BusinessException e, HttpServletRequest request) {
        return AjaxResult.error(-1,e.getMessage());
    }

    /**
     * 处理未知异常
     * @param e
     * @param request
     * @return
     */
    @ExceptionHandler(Exception.class)
    public AjaxResult handleException(Exception e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}',发生未知异常.", requestURI, e);
        return AjaxResult.error(-500,e.getMessage());
    }


}
